<%@page import="java.util.List"%>
<%@page import="com.jfinal.plugin.activerecord.Page"%>
<%@page import="com.hongye.app.model.AuthorModel"%>
<%@page import="com.hongye.app.model.MenuModel"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html class="fixed">
	<head>
		<!-- Basic -->
		<meta charset="UTF-8">
		<meta name="keywords" content="HTML5 Admin Template" />
		<meta name="description" content="Porto Admin - Responsive HTML5 Template">
		<meta name="author" content="okler.net">
		<!-- Mobile Metas -->
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<!-- Web Fonts  -->
		<!-- Vendor CSS -->
		<link rel="stylesheet" href="../admin/css/bootstrap.css" />
		<link rel="stylesheet" href="../admin/css/font-awesome/css/font-awesome.css" />
        <link rel="stylesheet" href="../admin/css/select2.css" />
		<!-- Theme CSS -->
		<link rel="stylesheet" href="../admin/css/theme.css" />
		<!-- Skin CSS -->
		<link rel="stylesheet" href="../admin/css/default.css" />
		<!-- Theme Custom CSS -->
		<!-- Theme Custom ../admin/css -->
		<script src="../admin/js/modernizr.js"></script>
	</head>
	<body>
		<section role="main" class="content-body" style="margin-top: 50px;">
			<!-- start: page -->
			<section class="panel">
				<header class="panel-heading">
					<h2 class="panel-title">权限管理</h2>
				</header>
				<div class="panel-body">
					<div class="row">
						<div class="col-sm-6">
							<div class="mb-md">
								<button id="addToTable" class="btn btn-primary" onclick="addauthshow();">Add <i class="fa fa-plus"></i></button>
							</div>
						</div>
					</div>
					<div id="addauth" style="display:none">添加权限:(选择所属菜单时，一定要把父菜单选上)<br>
						权限名称：<input id="auth_name" type="text" name="auth_name" value="" />
						所属控制器：<input id="auth_c" type="text" name="auth_c" value="" />
						所属方法名：<input id="auth_a" type="text" name="auth_a" value="" />
						级别：<input id="level" type="text" name="level" value="" /><p></p>
						<p>所属菜单</p>
						<% List<MenuModel> list=(List)request.getAttribute("menus");
							for(int i=1;i<=list.size();i++){
								MenuModel menu = list.get(i-1);
						%>
						<p><input type="checkbox" name="menuid" value='<%=menu.get("id")%>' /><%=menu.get("menu_name")%></p>
		                <%} %>
						<a class="btn btn-facebook mb-md ml-xs mr-xs" onclick="addauth();">确定添加 </a>
             		 	<a class="btn btn-facebook mb-md ml-xs mr-xs" onclick="addauthhid();">取消</a>
					</div>
					<table class="table table-bordered table-striped mb-none" id="datatable-default">
						<thead>
							<tr>
								<th class="center hidden-phone">序号</th>
								<th class="center hidden-phone">权限名称</th>
								<th class="center hidden-phone">控制器</th>
								<th class="center hidden-phone">所属方法</th>
								<th class="center hidden-phone">级别</th>
								<th class="center hidden-phone">操作</th>
							</tr>
						</thead>
						<tbody>
							<%List<AuthorModel> authview = (List)request.getAttribute("authView");
									for(int i=1;i<=authview.size();i++){
										AuthorModel auth = authview.get(i-1);
									%>
							<tr class="gradeA">
								<td class="center hidden-phone"><%=i %></td>
								<td class="center hidden-phone"><%=auth.get("auth_name") %></td>
								<td class="center hidden-phone"><%=auth.get("auth_c") %></td>
								<td class="center hidden-phone"><%=auth.get("auth_a") %></td>
								<td class="center hidden-phone"><%=auth.get("level") %></td>
								<td  class="center hidden-phone">
									<a href="./authDetail?id=<%=auth.get("id")%>" class="on-default edit-row"><i class="fa fa-pencil"></i></a>&nbsp;
									<a data-id="<%=auth.get("id") %>" onclick="delauth(this);" class="on-default remove-row" class="on-default edit-row"><i class="fa fa-trash-o"></i></a>
								</td>
							</tr>
							<%} %>
						</tbody>
					</table>
				</div>
			</section>
		</section>
		<!-- Specific Page Vendor -->
		<!-- Vendor -->
		<script src="js/jquery.js"></script>
		<script src="../admin/js/layer.js"></script>
		<script src="js/jquery-browser-mobile/jquery.browser.mobile.js"></script>
		<script src="js/bootstrap/js/bootstrap.js"></script>
		<script src="js/nanoscroller/nanoscroller.js"></script>
		<script src="js/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
		<script src="js/magnific-popup/magnific-popup.js"></script>
		<script src="js/jquery-placeholder/jquery.placeholder.js"></script>
		<!-- Specific Page Vendor -->
		<!--	<script src="js/select2.js"></script>-->
		<script src="js/jquery-datatables/media/js/jquery.dataTables.js"></script>
		<script src="js/jquery-datatables-bs3/assets/js/datatables.js"></script>
		<!-- Theme Base, Components and Settings -->
		<script src="js/theme.js"></script>
		<!-- Theme Custom -->
		<script src="js/theme.custom.js"></script>
		<!-- Theme Initialization Files -->
		<script src="js/theme.init.js"></script>
		<!-- Examples -->
		<script src="js/tables/examples.datatables.default.js"></script>
		<script src="js/tables/examples.datatables.row.with.details.js"></script>
		<script src="js/tables/examples.datatables.tabletools.js"></script>
		<script type="text/javascript">
		function addauthshow(){
			$("#addauth").css('display','block'); 
		}
		function addauthhid(){
			$("#addauth").css('display','none'); 
		}
		//添加
		function addauth(){
			var auth_name = $("#auth_name").val();
			var auth_c = $("#auth_c").val();
			var auth_a = $("#auth_a").val();
			var level = $("#level").val();
			var menuids = [];
			$('input[name="menuid"]:checked').each(function(){ 
				menuids.push($(this).val());
			}); 
			var url = '../admin/addAuth?auth_name='+auth_name+'&auth_c='+auth_c+'&auth_a='+auth_a+'&level='+level+'&menuids='+menuids;
			$.get(url,function(data){
				if(data=='suc'){
					location.href='../admin/authView';
				}else if(data=='fal'){
					alert('添加失败!');
				}else if(data=='exist'){
					alert('权限名已存在！');
				}
			});
		}
		
		//删除
		function delauth(obj){
			layer.confirm('确定删除吗？', {icon: 3, title:'提示'}, function(index){
				var id = obj.getAttribute("data-id");
				var url = '../admin/delAuth?id=' + id;
				$.get(url, function(data){
					if(data=='suc'){
						location.href='../admin/authView';
					}else if(data=='fal'){
						alert('删除失败!');
					}
				});
				  layer.close(index);
			});
		}
		</script>
		
	</body>
</html>